using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class Mp3Manager
	{
		#region CONSTRUCTORS
		static Mp3Manager()
		{
			_data = new Mp3Data(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static Mp3Data _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
		public static DataTable Insert( string mp3Name1,  string mp3Name2,  string mp3Name3,  int categoryID,  string mp3URL,  string mp3DemoURL,  int mp3Length,  string author,  string singer,  string mime,  string location,  long sMSCount,  string createdBy,  DateTime createdDate,  byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[15];
				values[0] = Util.ReplaceNullTo(mp3Name1, DBNull.Value);
				values[1] = Util.ReplaceNullTo(mp3Name2, DBNull.Value);
				values[2] = Util.ReplaceNullTo(mp3Name3, DBNull.Value);
				values[3] = categoryID;
				values[4] = Util.ReplaceNullTo(mp3URL, DBNull.Value);
				values[5] = Util.ReplaceNullTo(mp3DemoURL, DBNull.Value);
				values[6] = Util.ReplaceNullTo(mp3Length, DBNull.Value);
				values[7] = Util.ReplaceNullTo(author, DBNull.Value);
				values[8] = Util.ReplaceNullTo(singer, DBNull.Value);
				values[9] = Util.ReplaceNullTo(mime, DBNull.Value);
				values[10] = Util.ReplaceNullTo(location, DBNull.Value);
				values[11] = Util.ReplaceNullTo(sMSCount, DBNull.Value);
				values[12] = Util.ReplaceNullTo(createdBy, DBNull.Value);
				values[13] = Util.ReplaceNullTo(createdDate, DBNull.Value);
				values[14] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable Insert(Mp3Info info)
		{
			_error = "";
			try
			{
				object[] values = new object[15];
				values[0] = Util.ReplaceNullTo(info.Mp3Name1, DBNull.Value);
				values[1] = Util.ReplaceNullTo(info.Mp3Name2, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.Mp3Name3, DBNull.Value);
				values[3] = info.CategoryID;
				values[4] = Util.ReplaceNullTo(info.Mp3URL, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.Mp3DemoURL, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.Mp3Length, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.Author, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.Singer, DBNull.Value);
				values[9] = Util.ReplaceNullTo(info.Mime, DBNull.Value);
				values[10] = Util.ReplaceNullTo(info.Location, DBNull.Value);
				values[11] = Util.ReplaceNullTo(info.SMSCount, DBNull.Value);
				values[12] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
				values[13] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
				values[14] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static bool Update( long mp3ID,  string mp3Name1,  string mp3Name2,  string mp3Name3,  int categoryID,  string mp3URL,  string mp3DemoURL,  int mp3Length,  string author,  string singer,  string mime,  string location, byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[13];
				values[0] = mp3ID;
				values[1] = Util.ReplaceNullTo(mp3Name1, DBNull.Value);
				values[2] = Util.ReplaceNullTo(mp3Name2, DBNull.Value);
				values[3] = Util.ReplaceNullTo(mp3Name3, DBNull.Value);
				values[4] = categoryID;
				values[5] = Util.ReplaceNullTo(mp3URL, DBNull.Value);
				values[6] = Util.ReplaceNullTo(mp3DemoURL, DBNull.Value);
				values[7] = Util.ReplaceNullTo(mp3Length, DBNull.Value);
				values[8] = Util.ReplaceNullTo(author, DBNull.Value);
				values[9] = Util.ReplaceNullTo(singer, DBNull.Value);
				values[10] = Util.ReplaceNullTo(mime, DBNull.Value);
				values[11] = Util.ReplaceNullTo(location, DBNull.Value);
				values[12] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( Mp3Info info)
		{
			_error = "";
			try
			{
				object[] values = new object[13];
				values[0] = info.Mp3ID;
				values[1] = Util.ReplaceNullTo(info.Mp3Name1, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.Mp3Name2, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.Mp3Name3, DBNull.Value);
				values[4] = info.CategoryID;
				values[5] = Util.ReplaceNullTo(info.Mp3URL, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.Mp3DemoURL, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.Mp3Length, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.Author, DBNull.Value);
				values[9] = Util.ReplaceNullTo(info.Singer, DBNull.Value);
				values[10] = Util.ReplaceNullTo(info.Mime, DBNull.Value);
				values[11] = Util.ReplaceNullTo(info.Location, DBNull.Value);
				values[12] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( long mp3ID)
		{
			_error = "";
			try
			{
				object[] values = { mp3ID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( Mp3Info info)
		{
			_error = "";
			try
			{
				object[] values = { info.Mp3ID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static Mp3Info Select( long mp3ID)
		{
			_error = "";
			try
			{
				object[] values = { mp3ID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				Mp3Info info = new Mp3Info(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable SelectInfo(long mp3ID)
        {
            _error = "";
            try
            {
                object[] values = { mp3ID };
                return _data.Select(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return Mp3Manager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return Mp3Manager.Search( "", "", pageIndex, pageSize);
		}

        public static DataTable SearchBy(int intCateId, string strName, DateTime fromDate, DateTime toDate)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = intCateId;
                values[1] = strName;
                values[2] = fromDate;
                values[3] = toDate;

                return _data.SearchBy(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool Approve(int mp3Id, byte approveValue)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = mp3Id;
                values[1] = approveValue;
                return _data.Approve(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }
		
		#endregion
		
	}
}